package com.dooioo.pns.api.controller;

import com.dooioo.pns.business.service.v2.BusinessSideUnDisturbService;
import com.dooioo.pns.entity.model.v2.BusinessSideUnDisturb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * PackageName:com.dooioo.pns.api.controller
 * Author: Jerry.hu
 * Create: Jerry.hu (2015-07-28 下午7:29)
 * Description:
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping(value = "/api/v2/disturb")
public class BusinessUnDisturbController extends BaseController{
	@Autowired
	private BusinessSideUnDisturbService businessSideUnDisturbService;

	/**
	 * 设置勿扰模式
	 * @param businessSideUnDisturb 勿扰模式实体类
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST)
	public ResponseEntity<?> unDisturb(@RequestBody BusinessSideUnDisturb businessSideUnDisturb){
		Assert.isTrue(businessSideUnDisturb.getbId() > 0,"业务方分类id不可为空");
		Assert.isTrue(businessSideUnDisturb.getUserCode() > 0,"工号不可为空");
		Map<String,String> result = new HashMap<>();
 		try {
			if (businessSideUnDisturbService.insertInfo(businessSideUnDisturb)) {
				result.put("code","0");
				result.put("message","设置成功");
				return ResponseEntity.ok(result);
			} else {
				result.put("code","400");
				result.put("message","资源已存在");
				return new ResponseEntity<Object>(result, HttpStatus.BAD_REQUEST);
			}
		}catch (Exception e){
		    result.put("code","500");
		    result.put("message","服务器操作异常");
		    return new ResponseEntity<Object>(result, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	/**
	 * 设置勿扰模式
	 * @param businessSideUnDisturb 勿扰模式实体类
	 * @return
	 */
	@RequestMapping(method = RequestMethod.DELETE)
	public ResponseEntity<?> removeDisturb(@RequestBody BusinessSideUnDisturb businessSideUnDisturb){
		Assert.isTrue(businessSideUnDisturb.getbId() > 0,"业务方分类id不可为空");
		Assert.isTrue(businessSideUnDisturb.getUserCode() > 0,"工号不可为空");
		Map<String,String> result = new HashMap<>();
		try {
			if (businessSideUnDisturbService.removeInfo(businessSideUnDisturb)) {
				result.put("code","0");
				result.put("message","设置成功");
				return ResponseEntity.ok(result);
			} else {
				result.put("code","404");
				result.put("message","更新失败，资源不存在");
				return new ResponseEntity<Object>(result, HttpStatus.NOT_FOUND);
			}
		}catch (Exception e){
			result.put("code","500");
			result.put("message","服务器操作异常");
			return new ResponseEntity<Object>(result, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}


}
